
<template>
  <div class="role-add">
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="600px"
      :close-on-click-modal="false"
      @close="$reset('form')"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="form.name"></el-input>
        </el-form-item>
        <el-form-item label="数据权限等级">
          <el-select v-model="form.dataScope" placeholder="请选择数据权限等级">
            <el-option label="低权 - 仅自己" value="1"></el-option>
            <el-option label="中低权 - 本部门" value="3"></el-option>
            <el-option label="中权 - 本部门及以下" value="5"></el-option>
            <el-option label="中高权 - 自定义" value="7"></el-option>
            <el-option label="高权 - 所有" value="9"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="权限部门" v-if="form.dataScope == 7">
          <SelectDept :defaultVal="form.depts" @emit="setDeptData" />
        </el-form-item>
        <el-form-item label="排序" prop="orderBy">
          <el-input-number
            v-model="form.orderBy"
            :min="1"
            :max="1000"
            size="medium"
            label="排序"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="删除" prop="isDelete" v-if="flag">
          <template>
            <el-radio v-model="form.isDelete" label="0">可以删除</el-radio>
            <el-radio v-model="form.isDelete" label="1">不可删除</el-radio>
          </template>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" v-preventReClick @click="submitForm('form')"
          >提 交</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>
<script>
export default {
  components: {
    SelectDept: () => import('./SelectDept.vue'),
  },
  data() {
    return {
      flag: false, //开发者权限
      title: '新增',
      dialogVisible: false,
      form: {
        name: '',
        dataScope: '1',
        content: '',
        depts: '',
        type: '1',
        orderBy: '10',
        isDelete: '0', //可否删除
      },
      rules: {
        name: [{ required: true, message: '请输入角色名称', trigger: 'blur' }],
      },
    }
  },
  mounted() {
    this.getDeverMode()
    this.$bus.$on('showDialogRoleAdd', this.init)
  },
  beforeDestroy() {
    this.$bus.$off('showDialogRoleAdd')
  },
  methods: {
    //是否开发者模式
    async getDeverMode() {
      let userId = this.$store.state.userInfo.userInfo.id
      if (userId != 1) return
      let res = await this.$store.dispatch('getDictionaryVal', 'DEVER')
      this.flag = res[0] == 1
    },
    init(row) {
      if (row) {
        this.form = { ...this.form, ...row }
        this.$set(this.form, 'id', row.id)
      } else {
        this.form = this.$options.data().form
        this.$delete(this.form, 'id')
      }
      this.title = row ? '编辑' : '新增'
      this.dialogVisible = true
    },
    setDeptData(val) {
      this.form.depts = val.join(',')
    },
    submitForm(form) {
      this.$refs[form].validate(async (valid) => {
        if (valid) {
          let dispatchType = this.title == '新增' ? 'addTbRole' : 'updateTbRole'
          await this.$store.dispatch(dispatchType, this.form)
          this.done()
        }
      })
    },
    done() {
      this.dialogVisible = false
      this.$bus.$emit('refreshRoleList')
      this.$message({
        message: '操作成功',
        type: 'success',
      })
    },
  },
}
</script>

<style lang="scss" scoped>
.user-add {
  .form-add {
    position: relative;
    background-color: #fff;
    overflow: auto;
    padding-top: 20px;
    .el-form-item.top_unit i.el-icon-circle-close,
    .el-form-item.top_unit i.el-icon-arrow-down {
      position: absolute;
      right: 10px;
      top: 10px;
      cursor: pointer;
      color: #c0c4cc;
    }
    form.el-form {
      width: 800px;
    }
    .el-select {
      width: 100%;
    }
  }
  .btn-submit {
    padding-left: 100px;
    box-sizing: border-box;
  }
}
</style>